﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SFR
{
    public partial class Export : Form
    {
        MongoDbHelper mdb;
        Doc[] _docs;
        FolderBrowserDialog folderBrowserDialog1;
        BackgroundWorker bw;

        int filesProccessed;

        public Export(MongoDbHelper db,Doc[] docs)
        {
            folderBrowserDialog1 = new FolderBrowserDialog();
            mdb = db;
            _docs = docs;
            InitializeComponent();
            toolStripStatusLabel1.Text = "Ready!";

            bw = new BackgroundWorker();
            bw.WorkerSupportsCancellation = true;
            bw.WorkerReportsProgress = true;
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))
            {
                toolStripStatusLabel1.Text = "Downloaded: " + filesProccessed + "/" + _docs.Length;
                System.Windows.Forms.MessageBox.Show("Export cancelled!");
            }

            else if (!(e.Error == null))
            {
                toolStripStatusLabel1.Text = "Exported: " + filesProccessed + "/" + _docs.Length;
                System.Windows.Forms.MessageBox.Show("Error while exporting: " + e.Error);
            }
            else
            {
                progressBar1.Value = 100;
                toolStripStatusLabel1.Text = "Export successful. Downloaded: " + filesProccessed + "/" + _docs.Length;
            }

            buttonStart.Text = "Start";
            buttonStart.Enabled = true;
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            filesProccessed = 0;
            toolStripStatusLabel1.Text = "Export started, please wait ...";

            foreach (Doc item in _docs)
            {
                if ((bw.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }

                string fullPath;
                if (!textBox2.Text.EndsWith(Path.DirectorySeparatorChar.ToString()))
                    fullPath = textBox2.Text + Path.DirectorySeparatorChar + item.DocName;
                else
                    fullPath = textBox2.Text + item.DocName;

                mdb.ExportFile(item, fullPath);
                filesProccessed++;
                
                bw.ReportProgress(filesProccessed * (100 / _docs.Length), "Exported (" + filesProccessed +"/"+ _docs.Length +"): \"" + item.DocName + "\"...");
            }
        }

        private void buttonExport_Click(object sender, EventArgs e)
        {
            if (bw.IsBusy)
            {
                buttonStart.Enabled = false;
                bw.CancelAsync();
            }
            else
            {
                //export check
                if (_docs.Length == 0)
                {
                    System.Windows.Forms.MessageBox.Show("Error: No file for export selected!"); return;
                }
                if (!Directory.Exists(textBox2.Text))
                {
                    System.Windows.Forms.MessageBox.Show("Error: Export directory does not exists!"); return;
                }

                progressBar1.Value = 0;

                buttonStart.Text = "Cancel";
                bw.RunWorkerAsync();
            }
        }

        void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = (int)e.ProgressPercentage;
            toolStripStatusLabel1.Text = (string)e.UserState;
        }

        private void buttonBrowseExportFolder_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox2.Text = folderBrowserDialog1.SelectedPath;
            }
        }
    }
}
